package com.baize.filedowndemo.autoupdate

import android.app.Notification
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.baize.filedowndemo.R

/**
 * 前台服务
 */
class DownloadService : Service() {

  override fun onBind(intent: Intent?): IBinder? {
    return null
  }

  override fun onCreate() {
    super.onCreate()
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      setForegroundService();
    }
  }

  /**
   * 通过通知启动前台服务
   */
  private fun setForegroundService() {
    val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    val notification: Notification =
      NotificationCompat.Builder(this, "download").setContentTitle("自动更新")
        .setContentText("下载apk中...")
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_background))
        .setAutoCancel(false)
        .setOngoing(true)
        .build()
    startForeground(2,notification)
  }

}
